using System;
using System.Text ;
using System.Data ;
using System.Collections ;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.MF.DataAccess;
using WP.MF.DataEntity;

namespace WP.MF.BusinessRule
{
	/// <summary>
	/// Summary description for MProvinceBRL.
	/// </summary>
	public class MProvinceBRL:RuleBase
	{
        private string strThisClassName = "MProvinceBRL";

		public MProvinceBRL()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region CommonValid
		public bool CommonValid(DBAccess dBAccess,MProvinceDEL mProvinceDEL)
		{
			bool bReturn=true;
			bool bTemp=true;
			DataRow DR=mProvinceDEL.Tables[MProvinceDEL.TABLENAME_MPROVINCE].Rows[0];
			//check pst account
			if (DR[MProvinceDEL.PSTACCTNO].ToString().Trim()  !="" || DR[MProvinceDEL.PSTPROFITNO].ToString().Trim() !="" ) 
                bTemp=IsRecordExisted( dBAccess, "glAccount","AcctNo",DR[MProvinceDEL.PSTACCTNO].ToString(),"ProfitNo",DR[MProvinceDEL.PSTPROFITNO].ToString());

			if(!bTemp)
			{
                SysVar.ErrorMessage.Add(Msg.Select("PST Account not on file.", DataFunctions.GetMsg(dBAccess, this.GetType().Name, "MSG_PSTAccountNotOnFile")));
				bReturn &= bTemp;
			}
			
			//check GST account
			if (DR[MProvinceDEL.GSTACCTNO].ToString().Trim() !="" || DR[MProvinceDEL.GSTPROFITNO].ToString().Trim() !="" ) 
				bTemp=IsRecordExisted( dBAccess, "glAccount","AcctNo",DR[MProvinceDEL.GSTACCTNO].ToString(),"ProfitNo",DR[MProvinceDEL.GSTPROFITNO].ToString());

			if(!bTemp)
			{
                SysVar.ErrorMessage.Add(Msg.Select("GST Account not on file.", DataFunctions.GetMsg(dBAccess, this.GetType().Name, "MSG_GSTAccountNotOnFile")));
				bReturn &= bTemp;
			}

			//check ECO account
			if (DR[MProvinceDEL.ECOACCTNO].ToString().Trim() !="" || DR[MProvinceDEL.ECOPROFITNO].ToString().Trim() !="" ) 
				bTemp=IsRecordExisted( dBAccess, "glAccount","AcctNo",DR[MProvinceDEL.ECOACCTNO].ToString(),"ProfitNo",DR[MProvinceDEL.ECOPROFITNO].ToString());

			if(!bTemp)
			{
                SysVar.ErrorMessage.Add(Msg.Select("ECO Account not on file.", DataFunctions.GetMsg(dBAccess, this.GetType().Name, "MSG_ECOAccountNotOnFile")));
				bReturn &= bTemp;
			}
			

			return bReturn;
		}
		#endregion CommonValid

		#region Insert MProvince Valid
		public bool InsertValid(DBAccess dBAccess,MProvinceDEL mProvinceDEL)
		{
			bool bReturn=true;
            string strErrPrompt = string.Empty;
			SysVar.ErrorMessage.Clear();

            DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MProvinceDEL.TABLENAME_MPROVINCE + "'").DefaultView;
            foreach (DataRowView drw in dvwCheck)
            {
                if (!FieldCheck(dBAccess, mProvinceDEL.Tables[MProvinceDEL.TABLENAME_MPROVINCE].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.AddNew))
                {
                    bReturn = false;
                    if (SysVar.ErrorMessage.Count == 0)
                    {
                        SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                    }
                    break;
                }
            }
			return bReturn;
		}
		#endregion

		#region Update MProvince Valid
		public bool UpdateValid(DBAccess dBAccess,MProvinceDEL mProvinceDEL)
		{
			bool bReturn=true;
            string strErrPrompt = string.Empty; 
            SysVar.ErrorMessage.Clear();

			if(IsExistedAndNotChanged(dBAccess,mProvinceDEL.Tables[MProvinceDEL.TABLENAME_MPROVINCE]))
			{
                DataView dvwCheck = dBAccess.GetDataTable("select * from mnamedict where NeedCheck=1 and tablename='" + MProvinceDEL.TABLENAME_MPROVINCE + "'").DefaultView;
                foreach (DataRowView drw in dvwCheck)
                {
                    if (!FieldCheck(dBAccess, mProvinceDEL.Tables[MProvinceDEL.TABLENAME_MPROVINCE].Rows[0], drw["FieldName"].ToString(), ref strErrPrompt, FormStatus.Update))
                    {
                        bReturn = false;
                        if (SysVar.ErrorMessage.Count == 0)
                        {
                            SysVar.ErrorMessage.Add(new ErrInfo(strErrPrompt, drw["FieldName"].ToString()));
                        }
                        break;
                    }
                }
			}
			else
			{
				bReturn=false;
			}

			return bReturn;
		}

		#endregion

		#region Delete MProvince Valid
		public bool DeleteValid(DBAccess dBAccess,string strRecId,byte[] btOldRecStamp)
		{
			SysVar.ErrorMessage.Clear();

			//the error was msgboxed, so just exit here
            if (!base.IsExistedAndNotChanged(dBAccess, "MProvince", strRecId, btOldRecStamp))
            {
                return false;
            }

            if (MsgBoxEx.Show(Msg.Select("Are you sure to delete this state?", DataFunctions.GetMsg(this.GetType().Name, "MSG_DeleteThisState")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
            {
                return false;
            }
            return true;
		}

		#endregion

        #region Field Check
        public bool FieldCheck(DBAccess dbcon, DataRow dr, string strFieldName, ref string strErrPrompt, FormStatus formStatus)
        {
            strErrPrompt = string.Empty;
            if (strFieldName == null) return true;

            bool blnExistsField = false;
            for (int i = 0; i < dr.Table.Columns.Count; i++)
            {
                if (dr.Table.Columns[i].Caption.ToUpper().Trim() == strFieldName.Trim().ToUpper())
                {
                    blnExistsField = true;
                    break;
                }
            }
            if (!blnExistsField) return true;
            string strFieldValue = CvtFunc.ToString(dr[strFieldName.Trim()]);
            bool bHomogeneousTax = CvtFunc.ToBoolean(dr[MProvinceDEL.HOMOGENEOUSTAX]);
            string strGstAcctNo = CvtFunc.ToString(dr[MProvinceDEL.GSTACCTNO]);
            string strGstProfitNo = CvtFunc.ToString(dr[MProvinceDEL.GSTPROFITNO]);
            string strPstAcctNo = CvtFunc.ToString(dr[MProvinceDEL.PSTACCTNO]);
            string strPstProfitNo = CvtFunc.ToString(dr[MProvinceDEL.PSTPROFITNO]);
            DataRow drCheck = null;

            switch (strFieldName.ToUpper().Trim())
            {

                #region ProvNo
                case "PROVNO":
                    if (formStatus == FormStatus.AddNew)
                        return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MProvinceDEL.TABLENAME_MPROVINCE, strFieldName, ref  strErrPrompt);
                    else
                        return true;
                #endregion

                #region GstAcctNo, ProfitNo
                case "GSTACCTNO":
                case "GSTPROFITNO": //Gst account#
                    if (bHomogeneousTax)
                    {
                        drCheck = dbcon.GetDataFirstRow("chkglAccount '" + strGstAcctNo + "','" + strGstProfitNo + "','',0,2");
                        if (CvtFunc.ToInt(drCheck["number"].ToString()) <= 0) //<=0 show no found the Profit Center number
                        {
                            strErrPrompt =Msg.Select("HST Account not on file.", DataFunctions.GetMsg(this.GetType().Name, "MSG_HSTAcctNotOnFile"));
                            return false;
                        }
                    }
                    else
                    {
                        if (strGstAcctNo.Trim().Length > 0 || strGstProfitNo.Trim().Length > 0)
                        {
                            drCheck = dbcon.GetDataFirstRow("chkglAccount '" + strGstAcctNo + "','" + strGstProfitNo + "','',0,2");
                            if (CvtFunc.ToInt(drCheck["number"].ToString()) <= 0)
                            {
                                strErrPrompt =Msg.Select("Account does not exist!", DataFunctions.GetMsg("AcctNotExist"));
                                return false;
                            }
                        }
                    }
                    return true;
                #endregion
                #region PstAcctNo, ProfitNo
                case "PSTACCTNO":
                case "PSTPROFITNO": //Pst account#
                    if (strPstAcctNo.Trim().Length > 0 || strPstProfitNo.Trim().Length > 0)
                    {
                        drCheck = dbcon.GetDataFirstRow("chkglAccount '" + strPstAcctNo + "','" + strPstProfitNo + "','',0,2");
                        if (CvtFunc.ToInt(drCheck["number"].ToString()) <= 0) //<=0 show no found the Profit Center number
                        {
                            strErrPrompt = Msg.Select("Account does not exist!", DataFunctions.GetMsg("AcctNotExist"));
                            return false;
                        }
                    }
                    return true;
                #endregion
                #region RecyclingFeeAcctNo,RecyclingFeeProfitNo,RecyclingFeeBranchNo
                case "RECYCLINGFEEACCTNO":
                    if (dr["RecyclingFeeAcctNo"].ToString() == string.Empty && dr["RecyclingFeeProfitNo"].ToString() == string.Empty)
                    {
                        //strErrPrompt = Msg.Select("Account cannot be empty!", DataFunctions.GetMsg("AcctIsEmpty"));
                        return true;
                    }
                    else
                    {
                        if (!DataFunctions.CheckRecordExist(dbcon, "glAccount", dr["RecyclingFeeAcctNo"].ToString(), "AcctNo"))
                        {
                            strErrPrompt = Msg.Select("Account does not exist!", DataFunctions.GetMsg("AcctNotExist"));
                            return false;
                        }
                    }
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, "MCurrency", strFieldName, ref strErrPrompt);
                case "RECYCLINGFEEPROFITNO":
                    if (dr["RecyclingFeeProfitNo"].ToString() == string.Empty && dr["RecyclingFeeAcctNo"].ToString() == string.Empty)
                    {
                        //strErrPrompt = Msg.Select("Account cannot be empty!", DataFunctions.GetMsg("AcctIsEmpty"));
                        return true;
                    }
                    else
                    {
                        if (!DataFunctions.CheckRecordExist(dbcon, "glAccount", dr["RecyclingFeeProfitNo"].ToString(), "ProfitNo"))
                        {
                            strErrPrompt = Msg.Select("Account does not exist!", DataFunctions.GetMsg("AcctNotExist"));
                            return false;
                        }
                    }
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, "mProvince", strFieldName, ref strErrPrompt);
                //case "RECYCLINGFEEBRANCHNO":
                //    {
                //        if (dr["RecyclingFeeBranchNo"].ToString() == string.Empty)
                //        {
                //            //strErrPrompt = Msg.Select("Account cannot be empty!", DataFunctions.GetMsg("AcctIsEmpty"));
                //            return true;
                //        }
                //        else
                //        {
                //            if (!DataFunctions.CheckRecordExist(dbcon, "glAccount", dr["RecyclingFeeBranchNo"].ToString(), "BranchNo"))
                //            {
                //                strErrPrompt = Msg.Select("Account does not exist!", DataFunctions.GetMsg("AcctNotExist"));
                //                return false;
                //            }
                //        }
                //    }
                //    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, "mProvince", strFieldName, ref strErrPrompt);
                #endregion
                default:
                    return RuleBase.FieldValueCheckOne(dbcon, strFieldValue, MProvinceDEL.TABLENAME_MPROVINCE, strFieldName, ref strErrPrompt);
            }
        }
        #endregion

	}

}

